﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class Business_Cisoft3G_DraftboxManage_DraftboxManage : WebUI.Page.BasePage
{
    //定义页数
    private static int pageNum = 0;
    private DataTable dt;

    #region 页面加载
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ltScript.Text = "<script type=\"text/javascript\" src=\"../../../js/Basic/BasicPage.js\"></script><script type=\"text/javascript\" src=\"../../../js/My97DatePicker/WdatePicker.js\"></script>";
            chcAll.Attributes.Add("onclick", "SelectAllCheckboxes(this)");
            imgStartDate.Attributes.Add("onclick", "WdatePicker({el:'txtStartTime'})");
            imgEndDate.Attributes.Add("onclick", "WdatePicker({el:'txtEndTime'})");
            Bind();
        }
    }
    #endregion

    #region 数据绑定
    /// <summary>
    /// 数据绑定
    /// </summary>
    private void Bind()
    {
        //当前页码
        int cpn = AspNetPager1.CurrentPageIndex;
        //获取数据
        dt = new Cisoft.Cisoft3G.BLL.DraftsBO().GetListWithPage(cpn, 13, FillString()).Tables[0];
        pageNum = dt.Rows.Count;
        if (dt.Rows.Count > 0)
        {
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            AspNetPager1.RecordCount = new Cisoft.Cisoft3G.BLL.DraftsBO().GetListCount(cpn, 13, FillString());
            AspNetPager1.PageSize = 13;
        }
        else
        {
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            AspNetPager1.RecordCount = new Cisoft.Cisoft3G.BLL.DraftsBO().GetListCount(cpn, 13, FillString());
        }
    }
    #endregion

    #region 翻页事件
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
    }
    #endregion

    #region 查询事件
    /// <summary>
    /// 查询事件
    /// </summary>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Bind();
    }
    #endregion 

    #region 删除事件
    /// <summary>
    /// 删除事件
    /// </summary>
    protected void Button1_Click(object sender, EventArgs e)
    {
        List<int> list = new List<int>();
        foreach (RepeaterItem item in Repeater1.Items)
        {
            if (((CheckBox)item.FindControl("cbk_single")).Checked)
            {
                Label lblDraftID = item.FindControl("lblDraftID") as Label;
                if (!string.IsNullOrEmpty(lblDraftID.Text))
                {
                    list.Add(int.Parse(lblDraftID.Text));
                }
            }
        }

        Cisoft.Cisoft3G.BLL.DraftsBO draftsbo = new Cisoft.Cisoft3G.BLL.DraftsBO();
        string strError = string.Empty;
        if (list.Count > 0)
        {
            try
            {
                foreach (int id in list)
                {
                    draftsbo.Delete(id);
                }
            }
            catch (Exception ex)
            {
                strError = ex.Message;
            }
            if (string.IsNullOrEmpty(strError))
            {
                //PageRight("删除成功！", "DraftboxManage.aspx", "返回 ", "");
                PageRight(ModalPopupExtender1, lblTS, btnCancel, "<h4>删除成功！</h4>");
            }
            else
            {
                //PageError("删除失败，原因如下：" + strError + "。", Request.RawUrl);
                PageError(ModalPopupExtender1, lblTS, btnCancel, "删除失败，原因如下：" + strError);
            }
            Bind();
        }
    }
    #endregion

    #region 获得查询条件
    private string AppendString(string mobile, string content, string starttime, string endtime)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append(" 1=1 ");
        if (ddlSearchCondition.SelectedIndex == 0)
        {
            //sb.Append(string.IsNullOrEmpty(mobile) ? " " : " and DT_Receiver like '%" + mobile + "%' ");
            sb.Append(string.IsNullOrEmpty(mobile) ? "" : " and [DT_Receiver] in(select [CT_Mobile] from [CG_Contacts] where [CT_Name] like '%" +
               mobile + "%')or [DT_Receiver] in(select [DT_Receiver] from [CG_Drafts] where [DT_Receiver] like '%" + mobile + "%')");
        }
        else
        {
            sb.Append(string.IsNullOrEmpty(content) ? " " : " and DT_SmsContent like '%" + content + "%' ");
        }
        //sb.Append(string.IsNullOrEmpty(mobile) ? " " : " and DT_Receiver like '%" + mobile + "%' ");
        //sb.Append(string.IsNullOrEmpty(content) ? " " : " and DT_SmsContent like '%" + content + "%' ");
        sb.Append(string.IsNullOrEmpty(starttime) ? " " : " and DT_SendTime >= '" + starttime + "' ");
        sb.Append(string.IsNullOrEmpty(endtime) ? " " : " and DT_SendTime <= '" + endtime + "' ");
        return sb.ToString();
    }

    private string FillString()
    {
        string mobile = txtCondition.Value.Trim();
        string content = txtCondition.Value.Trim();
        string starttime = txtStartTime.Text.Trim();
        string endtime = txtEndTime.Text.Trim();
        return AppendString(mobile, content, starttime, endtime);
    }
    #endregion

    #region 详细事件
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Details")
        {
            Cisoft.Cisoft3G.Model.DraftsMO MO = new Cisoft.Cisoft3G.Model.DraftsMO();
            Cisoft.Cisoft3G.BLL.DraftsBO BO = new Cisoft.Cisoft3G.BLL.DraftsBO();
            MO = BO.GetModel(System.Convert.ToInt32(e.CommandArgument));
            if (MO != null)
            {
                //Cisoft.Cisoft3G.BLL.SmsTypeBO SmsTypebll = new Cisoft.Cisoft3G.BLL.SmsTypeBO();
                //Cisoft.Cisoft3G.Model.SmsTypeMO SmsTypemodel = new Cisoft.Cisoft3G.Model.SmsTypeMO();
                //lblType.Text = "普通";
                //SmsTypemodel = SmsTypebll.GetModel(int.Parse(MO.ST_SmsType.ToString()));
                //if (SmsTypemodel != null)
                //{
                //    Sentmodel.ST_SmsType = SmsTypemodel.ST_TypeName;
                //}


                lblType.Text = MO.DT_DraftName.Trim();
                lblSend.Text = "系统";
                lblRecive.Text = MO.DT_Receiver.Trim();
                string strTemp = string.Empty;
                string[] strs = lblRecive.Text.Trim().TrimEnd(',').Split(',');
                if (strs.Length > 0)
                {
                    foreach (string s in strs)
                    {
                        strTemp += string.Format("{0}({1}),",new Cisoft.Cisoft3G.BLL.ContactsBO().GetName(s),s);
                    }
                }
                lblRecive.Text = strTemp.TrimEnd(',');
                DateTime dt = Convert.ToDateTime(MO.DT_SendTime);
                lblSendTime.Text = dt.GetDateTimeFormats('f')[0].ToString();
                lblContent.Text = MO.DT_SmsContent.Trim();
                lblDraftID.Text = MO.DT_DraftID.ToString();
            }

            this.ModalPopupExtender3.Show();
        }
    }
    #endregion

    #region 显示详细信息
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        LinkButton lbtn = sender as LinkButton;
        this.ModalPopupExtender3.Show();
    }
    #endregion

    #region 发送
    protected void ToSend(object sender, EventArgs e)
    {
        Response.Redirect("../AddSms.aspx?id=" + lblDraftID.Text);
    }
    #endregion

    #region 选择删除提示
    //显示选择删除
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hihDelete.Value) && hihDelete.Value == "false")
        {
            lblTS.Text = "<br /><h4>请选择要删除的记录</h4><br />";
            btnConfirm2.Visible = true;
            btnCancel.Visible = false;
            ModalPopupExtender1.Show();
        }
        if (!string.IsNullOrEmpty(hihDelete.Value) && hihDelete.Value == "true")
        {
            lblTS.Text = "<br /><h4>你确认删除吗？</h4><br />";
            btnConfirm2.Visible = true;
            btnCancel.Visible = true;
            ModalPopupExtender1.Show();
        }
    }
    protected void btnConfirm2_Click(object sender, EventArgs e)
    {
        if (hihDelete.Value == "true")
        {
            Button1_Click(sender, e);
        }
        else
        {

        }
    }
    #endregion

    #region 数据绑定事件
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Cisoft.Cisoft3G.BLL.ContactsBO bllContactsShow = new Cisoft.Cisoft3G.BLL.ContactsBO();

        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            //姓名 类型
            Label lblSend = (Label)e.Item.FindControl("lblReceiver");
            string[] mobiles = DataBinder.Eval(e.Item.DataItem, "DT_Receiver").ToString().Trim().TrimEnd(',').Split(',');
            string strTemp = string.Empty;
            foreach (string s in mobiles)
            {
                strTemp += string.Format("{0}({1})", bllContactsShow.GetName(s),s) +",";   
            }
            lblSend.Text = strTemp + ",";
            lblSend.ToolTip = lblSend.Text;
            lblSend.Text = lblSend.Text.Length >= 40 ? lblSend.Text.TrimEnd(',').Substring(0, 37) + "..." : lblSend.Text.TrimEnd(',');
        }
    }
    #endregion
}
